﻿using Senparc.Weixin.MP.Containers;
using Senparc.Weixin.MP.Entities;
using Senparc.Weixin.MP.Entities.Request;
using Senparc.Weixin.MP.MessageHandlers;
using System;
using System.Web;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Senparc.Weixin.MP.AdvancedAPIs.Media;
using Senparc.Weixin.MP.AdvancedAPIs;
using HANS.WeChat;
using Senparc.Weixin.MP;

namespace HANS_WeChat.Base.Common
{
    /// <summary>
    /// 处理微信公众号的消息处理（如文本，语音，图像等）
    /// </summary>
    public class CustomMessageHandler: MessageHandler<CustomMessageContext>
    {

        public CustomMessageHandler(Stream inputStream, PostModel postModel) : base(inputStream, postModel)
        {
        }
        /// <summary>
        /// 默认处理
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase DefaultResponseMessage(IRequestMessageBase requestMessage)
        {
            var responseMessage = base.CreateResponseMessage<ResponseMessageText>(); //ResponseMessageText也可以是News等其他类型
            responseMessage.Content = "这条消息来自DefaultResponseMessage。";
            return responseMessage;
        }

        /// <summary>
        /// 文字处理
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnTextRequest(RequestMessageText requestMessage)
        {
            var responseMessage = base.CreateResponseMessage<ResponseMessageText>(); //ResponseMessageText也可以是News等其他类型
            switch (requestMessage.Content)
            {
                case "上传音乐":
                    var UploadTemporaryvoiceResult = MediaApi.UploadTemporaryMedia(WeChatBaseInfo.AppID, UploadMediaFileType.voice, System.Web.HttpContext.Current.Server.MapPath(@"~/content/Voice/语音消息.mp3"));
                    if (UploadTemporaryvoiceResult.errmsg == null)
                    {
                    
                        responseMessage.Content = "您上传了语音，mediadid='" + UploadTemporaryvoiceResult.media_id + "'";
                        return responseMessage;
                    }
                    break;
                case "上传永久音乐":
                    var UploadForvervoiceResult = MediaApi.UploadForeverMedia(WeChatBaseInfo.AppID, System.Web.HttpContext.Current.Server.MapPath(@"~/content/Voice/语音消息.mp3"));
                    if (UploadForvervoiceResult.errmsg == null)
                    {

                        responseMessage.Content = "您上传了永久语音，mediadid='" + UploadForvervoiceResult.media_id + "'";
                        return responseMessage;
                    }
                    break;

                case "上传图片":
                    var UploadTemporaryImageResult = MediaApi.UploadTemporaryMedia(WeChatBaseInfo.AppID, UploadMediaFileType.image,System.Web.HttpContext.Current.Server.MapPath(@"~/content/Image/login/head_120.png"));
                    if (UploadTemporaryImageResult.errmsg == null)
                    {

                        responseMessage.Content = "您上传了图片，mediadid='" + UploadTemporaryImageResult.media_id + "'";
                        return responseMessage;
                    }
                    break;

                case "上传永久图片":
                    var UploadForverimageResult = MediaApi.UploadTemporaryMedia(WeChatBaseInfo.AppID, UploadMediaFileType.image, System.Web.HttpContext.Current.Server.MapPath(@"~/content/Image/login/head_120.png"));
                    if (UploadForverimageResult.errmsg == null)
                    {

                        responseMessage.Content = "您上传了永久图片，mediadid='" + UploadForverimageResult.media_id + "'";
                        return responseMessage;
                    }
                    break;
                default: 
                    responseMessage.Content = "您发送了文本消息";
                    break;
                  
            }

            return responseMessage; ;
        }

        /// <summary>
        /// 语音的处理
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnVoiceRequest(RequestMessageVoice requestMessage)
        {
            var responseMessage = base.CreateResponseMessage<ResponseMessageVoice>(); //ResponseMessageText也可以是News等其他类型
            var mediaList= MediaApi.GetOthersMediaList(WeChatBaseInfo.AppID, UploadMediaFileType.voice, 1, 5);
            responseMessage.Voice.MediaId = mediaList.item.FirstOrDefault().media_id;
            //responseMessage.Voice.MediaId;
            return responseMessage;
        }

        /// <summary>
        /// 图片请求
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnImageRequest(RequestMessageImage requestMessage)
        {

            //var responseMessage = base.CreateResponseMessage<ResponseMessageImage>(); //ResponseMessageText也可以是News等其他类型
            //var mediaList = MediaApi.GetOthersMediaList(WeChatBaseInfo.AppID,UploadMediaFileType.image,1,5);
            //responseMessage.Image.MediaId = "Llmp3TGzDo9cHjB5ysGgTGFtg0YVqQve0AvzZCYfn0aMTeW0vELWbStpno89o0O0";
            var responseMessage = base.CreateResponseMessage<ResponseMessageText>(); //ResponseMessageText也可以是News等其他类型
            responseMessage.Content = "您发送了图片消息";
            return responseMessage;
        }

        /// <summary>
        /// 链接消息请求
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnLinkRequest(RequestMessageLink requestMessage)
        {
            var responseMessage = base.CreateResponseMessage<ResponseMessageText>(); //ResponseMessageText也可以是News等其他类型
            responseMessage.Content = "您发送了链接消息";
            return responseMessage;
        }

        /// <summary>
        /// 位置请求
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnLocationRequest(RequestMessageLocation requestMessage)
        {
            var responseMessage = base.CreateResponseMessage<ResponseMessageText>(); //ResponseMessageText也可以是News等其他类型
            responseMessage.Content = "您发送了位置请求";
            return responseMessage;
        }


        /// <summary>
        /// 短视频处理
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnShortVideoRequest(RequestMessageShortVideo requestMessage)
        {
            var responseMessage = base.CreateResponseMessage<ResponseMessageText>(); //ResponseMessageText也可以是News等其他类型
            responseMessage.Content = "您发送了短视频请求";
            return responseMessage;
        }


        /// <summary>
        /// 视频处理
        /// </summary>
        /// <param name="requestMessage"></param>
        /// <returns></returns>
        public override IResponseMessageBase OnVideoRequest(RequestMessageVideo requestMessage)
        {
            var responseMessage = base.CreateResponseMessage<ResponseMessageText>(); //ResponseMessageText也可以是News等其他类型
            responseMessage.Content = "您发送了视频请求";
            return responseMessage;
        }


        public override IResponseMessageBase OnEvent_ClickRequest(RequestMessageEvent_Click requestMessage)
        {
            return base.OnEvent_ClickRequest(requestMessage);
        }
        /// <summary> 
        /// 在我在处理消息之前触发
        /// </summary>
        public override void OnExecuting()
        {


            //var responseMessage = base.CreateResponseMessage<ResponseMessageText>(); //ResponseMessageText也可以是News等其他类型
            //responseMessage.Content = "您发送了请求之前";
            //CancelExcute = true;
            //ResponseMessage = responseMessage;//设置返回对象
            //if (RequestMessage.FromUserName == "")
            //{
            //    CancelExcute = true; //终止此用户的对话

            //    //如果没有下面的代码，用户不会收到任何回复，因为此时ResponseMessage为null

            //    //添加一条固定回复
            //    var responseMessage = CreateResponseMessage<ResponseMessageText>();
            //    responseMessage.Content = "Hey！你已经被拉黑啦！";

            //    ResponseMessage = responseMessage;//设置返回对象
            //}
        }

        public override void OnExecuted()
        {
            //var responseMessage = base.CreateResponseMessage<ResponseMessageText>(); //ResponseMessageText也可以是News等其他类型
            //responseMessage.Content = "您发送了请求之后";
            //ResponseMessage = responseMessage;//设置返回对象
        }

    }
}
